//
//  MWModifyNicknameController.m
//  XiRan-iOS
//
//  Created by liuyiming on 2019/10/31.
//  Copyright © 2019 Wood. All rights reserved.
//

#import "MWModifyNicknameController.h"

@interface MWModifyNicknameController () <UITextFieldDelegate>

@property(nonatomic, strong) UITextField *nicknameTF;
@property(nonatomic, strong) UIView *deviderView;
@property(nonatomic, strong) UILabel *hintLB;

@end

@implementation MWModifyNicknameController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = [[MWConfigManager manager] objectForKey:@"changename" defaultValue:@"更改名字"];
    if ([MWConfigManager manager].isNetError) {
        self.errorTitleLabel.text = [[MWConfigManager manager] objectForKey:@"click_retry" defaultValue:@"点击重试"];
        self.errorTitleLabel.hidden = self.errorImageView.hidden = NO;
    } else {
        [self setupPageSubviews];
    }
}

- (void)refresh {
    [super refresh];
    if ([MWConfigManager manager].isNetError) {
        self.errorTitleLabel.hidden = self.errorImageView.hidden = NO;
    } else {
        self.errorTitleLabel.hidden = self.errorImageView.hidden = YES;
        [self setupPageSubviews];
    }
}

- (void)saveAction:(UIButton *)sender {
    NSString *nickname = _nicknameTF.text;
    [self modifyNickname:nickname];
}

- (void)modifyNickname:(NSString *)nickname {
    NSString *token = [MWUserManager manager].user.token;
    NSMutableDictionary *paraDic = [[NSMutableDictionary alloc] init];
    [paraDic setObject:token forKey:@"token"];
    [paraDic setObject:nickname forKey:@"nickname"];
    [paraDic setObject:[MWConfigManager manager].language forKey:@"language"];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hud.label.text = [[MWConfigManager manager] objectForKey:@"loadin" defaultValue:@"加载中..."];
    
    MWWeakSelf
    [[MWNetworkManager manager] POST:kModifyNicknameUrl parameters:paraDic progress:^(NSProgress *downloadProgress) {
        
    } success:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSInteger code = [[dic objectForKey:@"code"] integerValue];
        NSString *msg = [dic objectForKey:@"msg"];
        
        hud.mode = MBProgressHUDModeText;
        hud.label.text = msg;
        [hud hideAnimated:YES afterDelay:2];
        
        if (code == 1) {
            [MWUserManager manager].user.nickname = weakSelf.nicknameTF.text;
            [self.navigationController popViewControllerAnimated:YES];
        }
    } failure:^(NSError *error) {
        [hud hideAnimated:YES afterDelay:2];
    }];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([@"\n" isEqualToString:string] == YES) {
        [self.view endEditing:NO];
        return NO;
    } else {
        if ([self stringContainsEmojiLast:string]) {
            return NO;
        }else{
            return YES;
        }
        return YES;
    }
}

//判断是否有emoji
-(BOOL)stringContainsEmojiLast:(NSString *)string {
    __block BOOL returnValue = NO;
    //九宫格 汉字 获取到的是圈123这样的
    //要可以输入
    if ([string isEqualToString:@"➋"]||[string isEqualToString:@"➌"]||[string isEqualToString:@"➍"]||[string isEqualToString:@"➎"]||[string isEqualToString:@"➏"]||[string isEqualToString:@"➐"]||[string isEqualToString:@"➑"]||[string isEqualToString:@"➒"]) {
        return returnValue;
    }
    
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        const unichar high = [substring characterAtIndex: 0];
        // Surrogate pair (U+1D000-1F9FF)
        if (0xD800 <= high && high <= 0xDBFF) {
            const unichar low = [substring characterAtIndex: 1];
            const int codepoint = ((high - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
            if (0x1D000 <= codepoint && codepoint <= 0x1F9FF){
                returnValue = YES;
            }
            // Not surrogate pair (U+2100-27BF)
        } else {
            if (0x2100 <= high && high <= 0x27BF){
                returnValue = YES;
            }
        }
    }];
    return returnValue;
}

- (void)setupPageSubviews {
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    saveBtn.frame = CGRectMake(0, 0, 20, 20);
    saveBtn.titleLabel.font = MWFont(14);
    [saveBtn setTitle:[[MWConfigManager manager] objectForKey:@"save" defaultValue:@"保存"] forState:UIControlStateNormal];
    [saveBtn setTitleColor:MWHexColor(@"#FFFFFF") forState:UIControlStateNormal];
    [saveBtn addTarget:self action:@selector(saveAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:saveBtn];
    self.navigationItem.rightBarButtonItem = buttonItem;
    
    _nicknameTF = [[UITextField alloc] init];
    _nicknameTF.text = [MWUserManager manager].user.nickname;
    _nicknameTF.font = MWFont(14);
    _nicknameTF.textColor = MWHexColor(@"#333333");
    _nicknameTF.delegate = self;
    [self.view addSubview:_nicknameTF];
       
    _deviderView = [[UIView alloc] init];
    _deviderView.backgroundColor = MWHexColor(@"#D8D8D8");
    [self.view addSubview:_deviderView];
    
    _hintLB = [[UILabel alloc] init];
    _hintLB.font = MWFont(10);
    _hintLB.textColor = MWHexColor(@"#999999");
    _hintLB.text = [[MWConfigManager manager] objectForKey:@"goodname" defaultValue:@"好名字可以让你的朋友更容易记住你"];
    [self.view addSubview:_hintLB];
    
    [_nicknameTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(16);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
    }];
    
    [_deviderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nicknameTF.mas_bottom).offset(7);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
        make.height.equalTo(@0.5);
    }];
    
    [_hintLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.deviderView.mas_bottom).offset(6);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
    }];
}


@end
